﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Text;

namespace Actuals.Data
{
    public class DalActual : Dal<IActual, Actual>, IDalActual
    {

        private readonly IDalManager _manager;
        private static readonly IDalActual _instance = new DalActual();
        private const string SP_GET_ACTUALS = "usp_GetActuals";
        private const string SP_Add_ACTUAL = "usp_AddActual";
        private const string SP_UPDATE_ACTUAL = "usp_UpdateActual";
        private const string SP_DELETE_ACTUAL = "usp_DeleteActual";
        private const string SP_GET_ACTUAL = "usp_GetActual";
        private const string SP_GET_ACTUAL_ONLY_FOR_DEVELOPERS = "usp_GetActualsOnlyForDevelopers";
        private const string SP_GET_ACTUALS_FOR_EMPLOYEE = "usp_GetActualsForEmployee";
        private const string SP_GET_ACTUALS_BY_TASKID = "usp_GetActualsByTaskId";
        

        public static IDalActual Instance
        {
            get { return _instance; }
        }

        public  DalActual()
        {
            _manager = new DalManager();
        }

        public DalActual(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IActual obj, DataRow row)
        {
            obj.ActualId = _manager.GetValue<Int64>(row, "ActualId");
            obj.ProjectId = _manager.GetValue<int>(row, "ProjectId");
            obj.ReleaseId = _manager.GetValue<int>(row, "ReleaseId");
            obj.EmployeeId = _manager.GetValue<int>(row, "EmployeeId");
            obj.TaskId = _manager.GetValue<int>(row, "TaskId");
            obj.SubTaskId = _manager.GetValue<int>(row, "SubTaskId");
            obj.Description = _manager.GetValue<string>(row, "Description");
            obj.date = _manager.GetValue<DateTime>(row, "ActualDate");
            obj.NoOfHours = _manager.GetValue<int>(row, "NoOfHours");
            obj.BASubTaskId = _manager.GetValue<int>(row, "BASubTaskId");
             obj.Nickname = _manager.GetValue<string>(row, "nickname");
             obj.TaskShortName = _manager.GetValue<string>(row, "taskshortname");
            // obj.subtaskdesc = _manager.GetValue<int>(row, "BASubTaskId");

        

        }

        
        public IList<IActual> GetActuals(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {
            _manager.StoredProcedureName = SP_GET_ACTUALS;
            _manager.AddInParameter("@FromDate", DbType.DateTime , fromDate);
            _manager.AddInParameter("@ToDate", DbType.DateTime, toDate);
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, releaseId);


            List<IActual> actuals = new List<IActual>();
            foreach (IActual actual in ConvertAll(_manager.ExecuteDataSet()))
                actuals.Add(actual);

            return actuals;
        }

        public IList<IActual> GetActualsByTaskId(DateTime FromDate, DateTime ToDate, int taskId)
        {
            _manager.StoredProcedureName = SP_GET_ACTUALS_BY_TASKID;
            _manager.AddInParameter("@taskid", DbType.Int32, taskId);
            _manager.AddInParameter("@FromDate", DbType.DateTime, FromDate);
            _manager.AddInParameter("@ToDate", DbType.DateTime, ToDate);
            
          


            List<IActual> actuals = new List<IActual>();
            foreach (IActual actual in ConvertAll(_manager.ExecuteDataSet()))
                actuals.Add(actual);

            return actuals;
        }

        public IList<IActual> GetActualsForEmployee(DateTime fromDate, DateTime toDate, int projectId, int releaseId, int employeeId)
        {
            _manager.StoredProcedureName = SP_GET_ACTUALS_FOR_EMPLOYEE;
            _manager.AddInParameter("@FromDate", DbType.DateTime, fromDate);
            _manager.AddInParameter("@ToDate", DbType.DateTime, toDate);
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, releaseId);
            _manager.AddInParameter("@EmployeeId", DbType.Int32, employeeId);



            List<IActual> actuals = new List<IActual>();
            foreach (IActual actual in ConvertAll(_manager.ExecuteDataSet()))
                actuals.Add(actual);

            return actuals;
        }

        public IList<IActual> GetActualsOnlyForDevelopers(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {
            _manager.StoredProcedureName = SP_GET_ACTUAL_ONLY_FOR_DEVELOPERS;
            _manager.AddInParameter("@FromDate", DbType.DateTime, fromDate);
            _manager.AddInParameter("@ToDate", DbType.DateTime, toDate);
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, releaseId);


            List<IActual> actuals = new List<IActual>();
            foreach (IActual actual in ConvertAll(_manager.ExecuteDataSet()))
                actuals.Add(actual);

            return actuals;
        }
    
        public IActual GetActual(Int64 actualId)
        {
            _manager.StoredProcedureName = SP_GET_ACTUAL;
            _manager.AddInParameter("@ActualId", DbType.Int64, actualId);

            IActual actual = new Actual();
            Convert(actual, _manager.ExecuteDataSet());

            return actual;
        }

        public bool AddActual(IActual actual)
        {
            _manager.StoredProcedureName = SP_Add_ACTUAL;

            _manager.AddInParameter("@ProjectId", DbType.Int32, actual.ProjectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, actual.ReleaseId);
            _manager.AddInParameter("@EmployeeId", DbType.Int32, actual.EmployeeId);
            _manager.AddInParameter("@TaskId", DbType.Int32, actual.TaskId);
            _manager.AddInParameter("@SubTaskId", DbType.Int32, actual.SubTaskId);
            _manager.AddInParameter("@BASubTaskId", DbType.Int32, actual.BASubTaskId);
            _manager.AddInParameter("@Description", DbType.String, actual.Description);
            _manager.AddInParameter("@date", DbType.DateTime, actual.date);
            _manager.AddInParameter("@NoOfHours", DbType.Int32, actual.NoOfHours);

            _manager.ExecuteNonQuery();

            return true;

        }

        public bool UpdateActual(IActual actual)
        {
            _manager.StoredProcedureName = SP_UPDATE_ACTUAL;

            _manager.AddInParameter("@ActualId", DbType.Int64, actual.ActualId);
            _manager.AddInParameter("@ProjectId", DbType.Int32, actual.ProjectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, actual.ReleaseId);
            _manager.AddInParameter("@EmployeeId", DbType.Int32, actual.EmployeeId);
            _manager.AddInParameter("@TaskId", DbType.Int32, actual.TaskId);
            _manager.AddInParameter("@SubTaskId", DbType.Int32, actual.SubTaskId);
            _manager.AddInParameter("@BASubTaskId", DbType.Int32, actual.BASubTaskId);
            _manager.AddInParameter("@Description", DbType.String, actual.Description);
            _manager.AddInParameter("@date", DbType.DateTime, actual.date);
            _manager.AddInParameter("@NoOfHours", DbType.Int32, actual.NoOfHours);

            _manager.ExecuteNonQuery();

            return true;

        }

        public bool DeleteActual(int actualId)
        {
            _manager.StoredProcedureName = SP_DELETE_ACTUAL;

            _manager.AddInParameter("@ActualId", DbType.Int64, actualId);

            _manager.ExecuteNonQuery();

            return true;

        }

    }
}
